在Unity中先寫好Interface的程式:
public interface IFirebaseAuth
{
void Initialize();
void Register(string email, string password, Action<bool, string> registerAction);
void Login(string email, string password, Action<FirebaseUser, string> userAction);
FirebaseUser GetUserInfo();
Task<bool> SendResetPasswordEmail(string email);
void SignOut();
Task<bool> SendVerificationEmail();
void UpdateUserInformation(UserProfile userProfile, Action<bool> updateAction);
void DeleteAccount();
bool CheckIsValid();
}
然後新增一個Class繼承IFirebaseAuth:
public class FirebaseAuth : IFirebaseAuth
{
public FirebaseAuth FireAuth;
public FirebaseUser CurrentLoginUser;
public void Initialize()
{
FireAuth = FirebaseAuth.DefaultInstance;
}
public async void Register(string email, string password, Action<bool, string> registerAction)
{
try
{
var authTask = await FireAuth.CreateUserWithEmailAndPasswordAsync(email, password);
var newUser = authTask.User;
Debug.LogFormat("Firebase Auth: Register successfully: {0}", newUser.Email);
registerAction?.Invoke(true, "Firebase Auth: Register successfully");
}
catch (Exception ex)
{
Debug.LogError($"Firebase Auth: Register encountered an error: {ex.Message}");
registerAction?.Invoke(false, $"Firebase Auth: Register encountered an error: {ex.Message}");
}
}
public async void Login(string email, string password, Action<FirebaseUser,string > userAction)
{
try
{
var authResult = await FireAuth.SignInWithEmailAndPasswordAsync(email, password);
Debug.LogFormat("Firebase Auth: Login successfully: {0}", authResult.User.Email);
CurrentLoginUser = authResult.User;
userAction?.Invoke(authResult.User, "Firebase Auth: Login successfully");
}
catch (Exception ex)
{
Debug.LogError($"Firebase Auth: Login encountered an error: {ex.Message}");
userAction?.Invoke(null, $"Firebase Auth: Login encountered an error: {ex.Message}");
}
}
public FirebaseUser GetUserInfo()
{
CurrentLoginUser = FireAuth.CurrentUser;
return CurrentLoginUser;
}
public async Task<bool> SendResetPasswordEmail(string email)
{
try
{
await FireAuth.SendPasswordResetEmailAsync(email);
Debug.Log("Firebase Auth: Password reset email sent successfully.");
return true;
}
catch (Exception ex)
{
Debug.LogError($"Firebase Auth: SendPasswordResetEmailAsync encountered an error: {ex.Message}");
return false;
}
}
public void SignOut()
{
FireAuth.SignOut();
}
public async Task<bool> SendVerificationEmail()
{
CurrentLoginUser = FireAuth.CurrentUser;
if (CurrentLoginUser != null)
{
try
{
await CurrentLoginUser.SendEmailVerificationAsync();
Debug.Log("Email verification sent successfully.");
return true;
}
catch (Exception ex)
{
Debug.LogError($"SendEmailVerificationAsync encountered an error: {ex.Message}");
return false;
}
}
Debug.LogError("Failed to send email verification: CurrentLoginUser is null.");
return false;
}
public async void UpdateUserInformation(UserProfile userProfile, Action<bool> updateAction)
{
if (CurrentLoginUser != null)
{
try
{
await CurrentLoginUser.UpdateUserProfileAsync(userProfile);
Debug.Log(CurrentLoginUser.DisplayName);
Debug.Log("User profile updated successfully.");
updateAction?.Invoke(true);
}
catch (Exception ex)
{
Debug.LogError($"UpdateUserProfileAsync encountered an error: {ex.Message}");
updateAction?.Invoke(false);
}
}
else
{
Debug.LogError("No user is currently signed in.");
updateAction?.Invoke(false);
}
}
public void DeleteAccount()
{
CurrentLoginUser.DeleteAsync();
Debug.Log("User Deleted successfully.");
}
public bool CheckIsValid()
{
return CurrentLoginUser != null && CurrentLoginUser.IsValid();
}
}
使用方法:
public IFirebaseAuth FirebaseAuth = new FirebaseAuth();
然後再需要的地方調用FirebaseAuth的Function即可。